package Prototype.Manager;

import java.util.HashMap;
import java.util.Scanner;

//抽象形状类
interface Shape extends Cloneable{
    public Object clone();
    public void countArea();
}
//具体形状
class Circle implements Shape{

    @Override
    public Object clone() {
        Circle c = null;
        try {
            c = (Circle)super.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return c;
    }

    @Override
    public void countArea() {
        double r = 0;
        System.out.println("这是⚪，请输入半径");
        Scanner input = new Scanner(System.in);
        r = input.nextDouble();
        System.out.println("面积："+3.14*r*r/2);
    }
}
class Square implements Shape{

    @Override
    public Object clone() {
        Square c = null;
        try {
            c = (Square)super.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return c;
    }

    @Override
    public void countArea() {
        double a = 0;
        System.out.println("这是⚪，请输入边长");
        Scanner input = new Scanner(System.in);
        a = input.nextDouble();
        System.out.println("面积："+a*a);
    }
}
//原型管理类
class Manager {
    private HashMap<String,Shape> ht= new HashMap<String,Shape>();
    //初始原型
    public Manager(){
        ht.put("Circle",new Circle());
        ht.put("Square",new Square());
    }
    //添加原型
    public void addShape(String key,Shape obj){
        ht.put(key,obj);
    }
    //根据键获取原型
    public Shape getShape(String key){
        Shape temp = ht.get(key);
        return (Shape)temp.clone();
    }
}
public class PrototypeManager {
    public static void main(String[] args) {
        Manager prom = new Manager();
        Shape obj1 = (Circle)prom.getShape("Circle");
        obj1.countArea();
        Shape obj2 = (Square)prom.getShape("Square");
        obj2.countArea();
    }
}
